/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package replic.console;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import replic.commons.Message;
import replic.commons.OutputMessage;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public abstract class RepliCTerminal extends Thread {

    protected List<Message> messageList;
    
    protected String replicHost;
    protected int replicPort;
    
    protected int terminalId;
    
    protected List<TransactionResult> transactionResults;
    
    protected boolean executing = false;
    
    protected String databaseName;
    
    public RepliCTerminal(int terminalId, File logFile, String databaseName, String replicHost, int replicPort) {
        executing = true;
        this.terminalId = terminalId;
        messageList = new ArrayList<Message>();
        this.databaseName = databaseName;
        this.replicHost = replicHost;
        this.replicPort = replicPort;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(logFile));
            String line = null;
            while ((line = reader.readLine()) != null) {
                Message message = new Message();
                message.setDatabase(databaseName);
                String[] temp = line.split("[|]");
                if (temp[3].equalsIgnoreCase("statement")) {
                    String sql = temp[5].trim();
                    message.setSql(sql);
                    if (sql.toLowerCase().startsWith("select")) {
                        message.setSqlType(Message.SQL_SELECT);
                    }
                    if (sql.toLowerCase().startsWith("insert")) {
                        message.setSqlType(Message.SQL_INSERT);
                    }
                    if (sql.toLowerCase().startsWith("update")) {
                        message.setSqlType(Message.SQL_UPDATE);
                    }
                    if (sql.toLowerCase().startsWith("delete")) {
                        message.setSqlType(Message.SQL_DELETE);
                    }
                } else {
                    if (temp[3].equalsIgnoreCase("commit")) {
                        message.setSqlType(Message.SQL_COMMIT);
                    }
                    if (temp[3].equalsIgnoreCase("rollback")) {
                        message.setSqlType(Message.SQL_ROLLBACK);
                    }
                }
                messageList.add(message);
            }
        } catch (IOException ex) {
            OutputMessage.println("RepliC Terminal " + terminalId + ": " + "ERROR: " + ex.getMessage());
            System.exit(0);
        }
        transactionResults = new ArrayList<TransactionResult>();
        OutputMessage.println("RepliC Terminal " + terminalId + ": " + messageList.size() + " SQL statement(s) loaded");
    }

    public List<Message> getMessageList() {
        return messageList;
    }

    public List<TransactionResult> getTransactionResults() {
        return transactionResults;
    }

    public boolean isExecuting() {
        return executing;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    
}